﻿using Api.Application.Command.User;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.User
{
    public class DelUserHandler : IRequestHandler<DelUserCommand, ApiResult<int>>
    {
        private readonly IBase<UserModel> userRepo;

        public DelUserHandler(IBase<UserModel> userRepo)
        {
            this.userRepo = userRepo;
        }
        public Task<ApiResult<int>> Handle(DelUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var user = userRepo.GetAll().Where(x => x.Id == request.Id).FirstOrDefault();
            user.IsDeleted = true;
            int r = userRepo.Update(user).Result;
            if (r > 0)
            {
                result.Msg = "删除成功";
                result.Code = ApiEnum.OK;
                result.Data = r;
            }
            else
            {
                result.Msg = "删除失败";
                result.Code = ApiEnum.OK;
                result.Data = r;
            }
            return Task.FromResult(result);
        }
    }
}
